package com.mz.ozy.mvp.presenter;

import android.app.Activity;

import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.CarGoodsBean;
import com.mz.ozy.bean.CarGoodsDetailBean;
import com.mz.ozy.bean.QiNiuBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.ReturnModel;
import com.mz.ozy.mvp.view.RetunContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.utils.QiNiuManger;
import com.mz.ozy.utils.RxUtil;
import com.mz.ozy.utils.SPUtils;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/12/12.
 */

public class ReturnPresenter extends BasePresenterImpl<RetunContract.View> implements RetunContract.Presenter {

    private ReturnModel mModel;

    private int flag;
    private List<String> imgs = new ArrayList<>();

    public ReturnPresenter() {
        mModel = new ReturnModel();
    }


    @Override
    public void onJu(Activity activity, String id, String company, String num) {
        mModel.getWl(activity, id, company, num)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {
                    @Override
                    public void onSuccesss(JSONObject bean) {
                        mView.onJh(bean);
                    }
                });
    }

    @Override
    public void onCanclInvite(Activity activity, String id) {
        mModel.getCanclInviteDetail(activity, id)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {
                    @Override
                    public void onSuccesss(JSONObject bean) {
                        mView.onCanclInvite(bean);
                    }
                });
    }

    @Override
    public void onlistDetail(Activity activity, String id) {
        mModel.getListDetail(activity, id)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<CarGoodsDetailBean>(mView) {
                    @Override
                    public void onSuccesss(CarGoodsDetailBean bean) {
                        mView.onlistDetail(bean);
                    }
                });
    }

    @Override
    public void onSDetail(Activity activity, String id) {
        mModel.getShDetail(activity, id)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<CarGoodsDetailBean>(mView) {
                    @Override
                    public void onSuccesss(CarGoodsDetailBean bean) {
                        mView.onShDetail(bean);
                    }
                });
    }

    @Override
    public void onlist(Activity activity, String page) {
        mModel.getList(activity, page)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<CarGoodsBean>(mView) {
                    @Override
                    public void onSuccesss(CarGoodsBean bean) {
                        mView.onlist(bean);
                    }
                });
    }

    @Override
    public void onUpReturn(Activity activity, String id, String apply_service, String refund_reason, String num, String instructions, List<QiNiuBean> list, int type, String url) {
        mView.showLoading();
        flag = 0;
        imgs.clear();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                imgs.add(list.get(i).getKey());
                QiNiuManger.getInstance().getUploadManager().put(list.get(i).getUrl(), list.get(i).getKey(), SPUtils.getInstance(activity).getString(Constants.SPName.QINIU_TOKEN, ""), new UpCompletionHandler() {
                    @Override
                    public void complete(String key, ResponseInfo info, JSONObject response) {
                        if (info.isOK()) {
                            flag++;
                            if (flag == list.size()) {
                                onAppeal1(activity, id, apply_service, refund_reason, num, instructions, type, url);
                            }
                        }
                    }
                }, null);
            }
        } else {
            onAppeal1(activity, id, apply_service, refund_reason, num, instructions, type, url);
        }
    }

    private void onAppeal1(Activity activity, String id, String apply_service, String refund_reason, String num, String instructions, int type, String url) {
        mModel.onUpdate(activity, id, apply_service, refund_reason, num, instructions, imgs, type, url)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {
                    @Override
                    public void onSuccesss(JSONObject bean) {
                        mView.onUpReturn(bean);
                    }
                });
    }

    public void onAppeal1(Activity activity, String id, String apply_service, String refund_reason, String num, String instructions, List<String> list, int type, String url) {
        mModel.onUpdate(activity, id, apply_service, refund_reason, num, instructions, list, type, url)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {
                    @Override
                    public void onSuccesss(JSONObject bean) {
                        mView.onUpReturn(bean);
                    }
                });
    }
}
